﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_Menu_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim data As New VSEntities
        Dim p As New VSModel.Menu
        Dim ParentMenuId As Integer = IIf(Request("MenuId") = "", 0, Request("MenuId"))
        Try
            p.Title = txtTitle.Text.Trim()
            p.TitlePage = txtMetaTitle.Text.Trim
            p.ImageLogo = hidImagePath.Text.Trim
            p.Description = txtDescription.Text.Trim()
            p.MenuPosition = CInt(drlMenuposition.SelectedValue)
            p.StatusId = 1
            'CInt(drlStatus.SelectedValue)

            p.Created = Date.Now
            p.Creator = Session("UserName")
            p.LanguageId = CInt(Session("LanguageId"))

            p.QueryString = makeURLFriendly(txtTitle.Text.Trim)
            p.ParentId = CInt(ParentMenuId)
            Dim parent As Integer = CInt(ParentMenuId)
            Dim t = (From h In data.Menus Where h.ParentId = parent Order By h.Sort Descending Select h).FirstOrDefault
            If Not t Is Nothing Then
                p.Sort = CInt(t.Sort.ToString) + 1
            Else
                p.Sort = 1
            End If

            Data.Menus.AddObject(p)
            data.SaveChanges()
            Insert_App_Log("Create Menu:" & txtTitle.Text.Trim & "", Function_Name.Menu, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
            Excute_Javascript("Alertbox('Thêm mới dữ liệu thành công.');window.location ='../../Page/Menu/List.aspx?MenuId=" & Request("MenuId") & "';", Me.Page, True)

        Catch ex As Exception
            log4net.Config.XmlConfigurator.Configure()

            log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
            Excute_Javascript("Alertbox('Cập nhật thất bại.');window.location ='../../Page/Menu/List.aspx';", Me.Page, True)
        End Try
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then


            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


            Dim ctrl As System.Web.UI.WebControls.WebControl
            ctrl = txtDescription.CreateCommandButton("ask_mask_fullname", "remove_format.gif", "Bỏ định dạng")
            ctrl.Attributes("onclick") = "removeTextFormat('" + txtDescription.ClientID + "');"
            'get the pos after the Italic    
            Dim pos As Integer
            pos = txtDescription.ToolControls.IndexOf("JustifyRight") + 1
            'add this custom button into the editor    
            txtDescription.InsertToolControl(pos, "ask_mask_fullname", ctrl)
        End If
    End Sub
End Class
